package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.ibatis.annotations.Param;
import com.ruoyi.system.domain.SysNoticeRead;
import com.ruoyi.system.mapper.SysNoticeReadMapper;
import com.ruoyi.system.service.ISysNoticeReadService;

/**
 * 通知公告阅读记录 服务层实现
 * 
 * @author ruoyi
 */
@Service
public class SysNoticeReadServiceImpl implements ISysNoticeReadService
{
    @Autowired
    private SysNoticeReadMapper noticeReadMapper;

    /**
     * 查询公告阅读记录信息
     * 
     * @param id 主键ID
     * @return 阅读记录信息
     */
    @Override
    public SysNoticeRead selectNoticeReadById(Long id)
    {
        return noticeReadMapper.selectNoticeReadById(id);
    }

    /**
     * 查询公告阅读记录列表
     * 
     * @param noticeRead 阅读记录信息
     * @return 阅读记录集合
     */
    @Override
    public List<SysNoticeRead> selectNoticeReadList(SysNoticeRead noticeRead)
    {
        return noticeReadMapper.selectNoticeReadList(noticeRead);
    }

    /**
     * 根据公告ID查询阅读记录列表
     * 
     * @param noticeId 公告ID
     * @return 阅读记录集合
     */
    @Override
    public List<SysNoticeRead> selectNoticeReadByNoticeId(Long noticeId)
    {
        return noticeReadMapper.selectNoticeReadByNoticeId(noticeId);
    }



    /**
     * 查询用户是否已读指定公告
     * 
     * @param noticeId 公告ID
     * @param userId 用户ID
     * @return 阅读记录
     */
    @Override
    public SysNoticeRead selectNoticeReadByNoticeIdAndUserId(Long noticeId, Long userId)
    {
        return noticeReadMapper.selectNoticeReadByNoticeIdAndUserId(noticeId, userId);
    }

    /**
     * 新增公告阅读记录
     * 
     * @param noticeRead 阅读记录信息
     * @return 结果
     */
    @Override
    public int insertNoticeRead(SysNoticeRead noticeRead)
    {
        noticeRead.setCreateTime(new Date());
        if (noticeRead.getReadTime() == null) {
            noticeRead.setReadTime(new Date());
        }
        return noticeReadMapper.insertNoticeRead(noticeRead);
    }

    /**
     * 修改公告阅读记录
     * 
     * @param noticeRead 阅读记录信息
     * @return 结果
     */
    @Override
    public int updateNoticeRead(SysNoticeRead noticeRead)
    {
        return noticeReadMapper.updateNoticeRead(noticeRead);
    }

    /**
     * 删除公告阅读记录信息
     * 
     * @param id 主键ID
     * @return 结果
     */
    @Override
    public int deleteNoticeReadById(Long id)
    {
        return noticeReadMapper.deleteNoticeReadById(id);
    }

    /**
     * 根据公告ID删除阅读记录
     * 
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeReadByNoticeId(Long noticeId)
    {
        return noticeReadMapper.deleteNoticeReadByNoticeId(noticeId);
    }

    /**
     * 批量删除公告阅读记录信息
     * 
     * @param ids 需要删除的主键ID
     * @return 结果
     */
    @Override
    public int deleteNoticeReadByIds(Long[] ids)
    {
        return noticeReadMapper.deleteNoticeReadByIds(ids);
    }

    /**
     * 统计公告阅读情况
     * 
     * @param noticeId 公告ID
     * @return 统计结果
     */
    @Override
    public SysNoticeRead selectNoticeReadStatistics(Long noticeId)
    {
        return noticeReadMapper.selectNoticeReadStatistics(noticeId);
    }

    /**
     * 标记公告为已读
     * 
     * @param noticeId 公告ID
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int markNoticeAsRead(@Param("noticeId") Long noticeId, @Param("userId") Long userId)
    {
        // 检查是否已经标记为已读
        SysNoticeRead existingRead = selectNoticeReadByNoticeIdAndUserId(noticeId, userId);
        if (existingRead != null) {
            return 1; // 已经标记为已读
        }

        // 新增阅读记录
        SysNoticeRead noticeRead = new SysNoticeRead();
        noticeRead.setNoticeId(noticeId);
        noticeRead.setUserId(userId);
        noticeRead.setReadTime(new Date());
        return insertNoticeRead(noticeRead);
    }
}
